﻿using System;

namespace Infrastructure.Config.Core.EventBus
{
    public class RetrySettings
    {
        /// <summary>
        /// 重试次数
        /// </summary>
        public int RetryLimit { get; set; }

        /// <summary>
        /// 重试时间最小间隔, 示例：TimeSpan.FromSeconds(5)
        /// </summary>
        public int RetryMinIntervalInSeconds { get; set; }

        /// <summary>
        /// 重试时间最小间隔, 示例：TimeSpan.FromMinutes(2)
        /// </summary>
        public int RetryMaxIntervalInSeconds { get; set; }

        /// <summary>
        /// 重试开始的时间间隔, 示例：TimeSpan.FromSeconds(5)
        /// </summary>
        public TimeSpan RetryIntervalDelta { get; set; }
    }

    public class RateLimitSettings
    {
        /// <summary>
        /// 指定时间段内限速次数
        /// </summary>
        public int RateLimit { get; set; }

        /// <summary>
        /// 限速评测时间段, 示例：TimeSpan.FromMinutes(1)
        /// </summary>
        public int RateLimitIntervalInSeconds { get; set; }
    }

    public class CircuitBreakerSettings
    {
        /// <summary>
        /// 重置失败次数的时间段
        /// </summary>
        public int CircuitBreakerTrackingPeriodInSeconds { get; set; }

        /// <summary>
        /// 设定触发断路器之前失败次数达到的百分比阀值
        /// </summary>
        public int CircuitBreakerTripThreshold { get; set; }

        /// <summary>
        /// 设定触发断路器之前必须达到的失败次数阀值
        /// </summary>
        public int CircuitBreakerActiveThreshold { get; set; }
    }

    public class EventBusSettings
    {
        public string HostUri { get; set; }

        /// <summary>
        /// 虚拟主机, 用于隔离不同的模块或项目
        /// </summary>
        public string VHost { get; set; }

        /// <summary>
        /// 交换器
        /// </summary>
        public string Exchange { get; set; }


        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }


        #region 重试配置, 示例：消息消费失败后重试3次，从5秒到2分钟指数组增长 => config.UseRetry(r => r.Exponential(3, TimeSpan.FromSeconds(5), TimeSpan.FromMinutes(2), TimeSpan.FromSeconds(5)));
        /// <summary>
        /// 是否启用重试
        /// </summary>
        public bool IsRetryEnabled { get; set; }
        public RetrySettings RetrySettings { get; set; }

        #endregion

        #region 限速配置, 示例：每分钟消息消费数限定在1000之内 => config.UseRateLimit(1000, TimeSpan.FromSeconds(1));
        /// <summary>
        /// 是否启用限速
        /// </summary>
        public bool IsRateLimitEnabled { get; set; }
        public RateLimitSettings RateLimitSettings { get; set; }

        #endregion

        #region 断路器: 防止跨多个系统的这种灾难性级联故障
        /// <summary>
        /// 是否启用断路器
        /// </summary>
        public bool IsCircuitBreakerEnabled { get; set; }
        public CircuitBreakerSettings CircuitBreakerSettings { get; set; }
        #endregion
    }
}